package com.vip.jwt.entity;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
import java.io.Serializable;
import java.util.List;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author vip
 * @since 2021-05-04
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_user")
@ApiModel(value="User对象", description="用户表")
public class User implements Serializable {

    private static final long serialVersionUID=1L;

    @ApiModelProperty(value = "ID")
    private String id;

    @ApiModelProperty(value = "用户名")
    @NotBlank(message = "账号不能为空")
    @Length(min = 3,message = "账号不能低于6位字符")
    private String username;

    @ApiModelProperty(value = "密码")
    @NotBlank(message = "密码不能为空")
    @Length(min = 6,message = "密码不能低于6位字符")
    private String password;

    @ApiModelProperty(value = "盐")
    private String salt;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @ApiModelProperty(value = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @TableField(exist = false)
    private List<Role> roles;

    /**
     * 将密码加密
     * @param password
     * @return
     */
    public static String encryptPassword(String password){
        String salt = RandomUtil.randomString(5);
        Md5Hash md5Hash = new Md5Hash(password,salt,2);
        return md5Hash.toString();
    }

    /**
     * 将密码加密
     * @param password
     * @param salt
     * @return
     */
    public static String encryptPassword(String password,String salt){
        Md5Hash md5Hash = new Md5Hash(password,salt,2);
        return md5Hash.toString();
    }

}
